﻿using LitJson;
using LNet;
using OestsCommon.Model;
using OestsDataBase;
using OestsServer.Net;
using System.Collections.Generic;
using System.Linq;

namespace OestsServer.Handlers.OESPHandlers
{
    public class UserTableHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.UserTable;
            }
        }


        public override void Execute(OESPClientChannel client, Request packet)
        {
            ResponsePacket = new Response(packet);

            //读取数据库
            List<User> list = UserDAL.GetUsers();
            if (list != null)
            {
                //教员在线检查//TODO:检查在线状态
                //list.ForEach(s =>
                //{
                //    s.OnLine = client.CheckUserIsExist(UserType.Teacher, s.DBid);
                //});
            }
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }
    }

    public class AddUserHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.AddUser;
        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            List<User> users = JsonMapper.ToObject<List<User>>(resquestPacket.DataStr);
            foreach (var item in users)
            {
                int id = UserDAL.Add(item);
                if (id > 0)
                {
                    item.DBid = id;
                }
            }

            ResponsePacket.ReturnCode = ReturnCode.Success;
            ResponsePacket.DataObject = users;
            client.Send(ResponsePacket);
        }
    }

    public class DeleteUserHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get { return PacketKey.DeleteUser; }
        }
        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            //解析数据
            DP<UserType, List<int>> data = JsonMapper.ToObject<DP<UserType, List<int>>>(packet.DataStr);
            UserDAL.DeleteUsers(data.Value2);

            if (data.Value1 == UserType.Student)
            {
                //删除学生相关的考试信息
                StudentExamDAL.DeleteStudentInfos(data.Value2);
                //删除学生相关的实训信息
                TrainDAL.DeleteStudentInfos(data.Value2);
            }
            else if (data.Value1 == UserType.Teacher)
            {
                //删掉教师创建的试卷
                ExamDAL.DelExamByTeacherIDs(data.Value2);
            }
            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);
            /*
            List<int> bakc = data.Value2.Where(i =>
            {
                if (UserDAL.DeleteUser(i))
                {
                    if (data.Value1 == UserType.Student)
                    {
                        //删除学生相关的考试信息
                        StudentExamDAL.DeleteStudentInfo(i);
                        //删除学生相关的实训信息
                        TrainDAL.DeleteStudentInfo(i);
                    }
                    else if (data.Value1 == UserType.Teacher)
                    {
                        //删掉教师创建的试卷
                        ExamDAL.DelExamByTeacherID(i);
                    }
                    return true;
                }
                return false;
            }
            ).ToList();


            ResponsePacket.DataObject = new DP<UserType, List<int>>(data.Value1, bakc);
            client.Send(ResponsePacket);
            */
        }
    }

    /// <summary>
    /// 修改数据
    /// </summary>
    public class ModifyUserHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.ModifyUser;
            }
        }

        /// <summary>
        /// 只修改用户-只修改姓名，密码，工号
        /// </summary>
        /// <param name="client"></param>
        /// <param name="datas"></param>
        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<User> list = JsonMapper.ToObject<List<User>>(packet.DataStr);

            List<User> back = list.Where(t =>
            {
                if (UserDAL.CheckExist(t))
                    return false;
                return UserDAL.ModifyUser(t);
            }

            ).ToList();
            ResponsePacket.DataObject = back;
            client.Send(ResponsePacket);
        }
    }

    public class ModifyPWDHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.ModifyPWD;
            }
        }

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            ResponsePacket = new Response(packet);
            string pwd = packet.DataStr;
            string pwdDb = UserDAL.GetPassword(client.User.DBid);
            if (pwd == "")
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "密码不能为空";
            }
            else if (pwdDb == pwd)
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "新密码和旧密码不能相同";
            }
            else
            {
                UserDAL.ModifyPwd(pwd, client.User.DBid);
                ResponsePacket.ReturnCode = ReturnCode.Success;
                ResponsePacket.RetrunMessage = "修改成功";
            }

            client.Send(ResponsePacket);
        }
    }
}